﻿#region Imports
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using CommonUtilities.Extensions;
#endregion

namespace BulkRenamer.Common.Models
{
    /// <summary>
    /// Common entity structure following Id-Name-Value format
    /// </summary>
    [Serializable]
    public class IdNameValueMember
    {
        #region Properties
        /// <summary>
        /// Unique Id
        /// </summary>
        public object Id { get; set; }
        /// <summary>
        /// Name
        /// </summary>
        public object Name { get; set; }
        /// <summary>
        /// Value
        /// </summary>
        public object Value { get; set; }
        /// <summary>
        /// Description
        /// </summary>
        public string Description { get; set; }
        #endregion

        #region Methods
        public static ObservableCollection<IdNameValueMember> GetMembersFromSettings(object sett)
        {
            // RKD
            //ObservableCollection<IdNameValueMember> allExtn = new ObservableCollection<IdNameValueMember>();
            //List<string> sAllExtn = new List<string>();
            //
            //sAllExtn.AddRange(((StringCollection)BulkRenamerSettings[sett]).Cast<string>());
            //allExtn = new ObservableCollection<IdNameValueMember>(
            //    sAllExtn.Distinct().Where(ext => !Utilities.IsNull(CrNew(ext))).Select(ext => CrNew(ext)));
            //allExtn = Utilities.AddCollectionIndex<IdNameValueMember>(allExtn, "Id").ToList();

            List<IdNameValueMember> allMembers = new List<IdNameValueMember>();
            allMembers = new List<IdNameValueMember>(
                ((StringCollection)sett).Cast<string>().Select(ext => new IdNameValueMember { Id = ext }));
            allMembers = allMembers.Distinct().Select(ext => CreateNewMember(Convert.ToString(ext.Id))).Where(ext => !ext.IsNull()).ToList();
            allMembers.AddCollectionIndex<IdNameValueMember>("Id").ToList();

            return new ObservableCollection<IdNameValueMember>(allMembers);
        }

        private static IdNameValueMember CreateNewMember(string val)
        {
            string[] spltResult = val.Split(',');
            IdNameValueMember idv = null;

            if (spltResult.HasCount(3))
            {
                if (spltResult[0].Length * spltResult[1].Length * spltResult[2].Length > 0)
                {
                    idv = new IdNameValueMember
                            {
                                Name = spltResult[0].ToLower(),
                                Value = spltResult[1],
                                Description = spltResult[2]
                            };
                }
            }

            return idv;
        }
        #endregion
    }
}
